<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 题目3(必做):数组去重(可以用多种方法,至少写一种)
      // 要求：输出一个数组：[1,2,3,9]
      const arr2 = [1, 2, 3, 2, 9]

      // 1. Set数据结构
      const arr = [...new Set(arr2)]
      console.log(arr)

      // 2，reduce
      const res = arr2.reduce(function(sum, item){
        if (!sum.includes(item)) {
          return sum.concat(item)
        } else {
          return sum
        }
      }, [])  
      console.log(res);
      
      // 3. indexOf
      const arr3 = []
      arr2.forEach(item => {
        if(arr3.indexOf(item)===-1){
          arr3.push(item)
        }
      })
      console.log(arr3);

      // 4. 双重for循环
      for(let i = 0;i<arr2.length;i++){
        for(let j = 0;j<arr2.length;j++){
          if(arr2[i] === arr[j] && i != j){
            arr2.splice(i,1)
          }
        }
      }
      console.log(arr2);
    </script>
  </body>
</html>
